08. Demo: Backtesting

PRDTM2-787 AI Trading C4 L4 Demo 1

Overview of Backtesting S&P 500 Using SQLite

Gain practical insight into backtesting a trading strategy using historical S&P 500 data. Utilizing a SQLite database simplifies storing stock prices and tracking results, enabling efficient analysis.

Key Steps in the Backtesting Process:

  1. Data Setup

    • Use closing values of S&P 500 from January 2, 2019, to May 31, 2021.
    • Store data in a SQLite database for easy access.
  2. Model Implementation

    • Employ a Geometric Brownian Motion (GBM) model for forecasting.
    • Set random seed for reproducibility.
  3. Database & Table Management

    • Connect to SQLite, creating a table to record trades.
    • Initial capital inserted into the table for backtesting starting conditions.
  4. Trading Strategy Functionality

    • A simple algorithm based on comparing current price with forecast bounds.
    • Buy when price is below forecast; sell when above.
  5. Execution and Results

    • Load historical prices, prepare tables, and execute backtest.
    • Review results showing a modest capital growth, signaling need for refined risk management strategies.

By following these steps, learners will grasp essentials of running backtests for trading strategies efficiently.

PRDTM2-787 AI Trading C4 L4 Demo 2

Calculating Expected Shortfall with Python

Understand expected shortfall (ES) and learn to calculate it using Python. ES measures potential loss for a given confidence level when losses exceed Value at Risk (VaR). It provides insight into potential severe losses from investments. A high confidence level (e.g., 95%) offers the probability of VaR being exceeded (5%). Here's an overview:

  • Expected Shortfall Definition

    • Indicates potential loss exceeding VaR.
    • Calculated at a specified confidence level (e.g., 95%).
  • Formulas and Components

    • ES Formula: Uses mean (Mu) and standard deviation (Sigma) of profit/loss distribution.
    • Assumes normal distribution for potential losses.
    • Utilizes probability density (ϕ) and cumulative distribution (Φ) functions.
  • Practical Example

    • Estimating a stock's expected shortfall over a month, considering annual return distribution.
    • Calculate relative return using Brownian motion model.
  • Risk Management Strategies

    • Reduce exposure or hedge investments to manage risks identified by ES calculations.

Understanding and applying these concepts helps in crafting robust trading and risk management strategies.